﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Stu_sys.Entity;
using Stu_sys.Models;
using Stu_sys.utils;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Reflection;
using System.Text.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace Stu_sys.Controllers
{
    public class DepartmentController : Controller
    {
        private readonly MysqlContext _mysqlContext;
        public DepartmentController(MysqlContext context) 
        {
            _mysqlContext = context;
        }

        public IActionResult DepartmentList()
        {
            return View();
        }


        [HttpGet]
        public IActionResult GetDepList()
        {
            var attrStr = MyUtils.GetAttributeToJsonStr(typeof(DepViewModel));
            var depList = _mysqlContext.Departments.ToList();
            var roleStr = JsonSerializer.Serialize(depList);
            var data = new
            {
                attr = attrStr,
                row = roleStr
            };
            return Json((data));
        }

        [HttpPost]
        public IActionResult AddDep(Department department) 
        {
            if (department.DepName == "" || department.DepCode == "")
            {
                return Json(-1);
            }
            if (IsExistDep(department))
            {
                return Json(0);
            }
            _mysqlContext.Departments.Add(department);
            var res = _mysqlContext.SaveChanges();
            return Json(res);
        }

        [HttpPost]
        public IActionResult EditDep(Department department)
        {
            if (department.DepName == "" || department.DepCode == "")
            {
                return Json(-1);
            }

            var dep=_mysqlContext.Departments.Where(d=>d.DepartmentId==department.DepartmentId).FirstOrDefault();
            dep.DepName=department.DepName;
            dep.DepCode=department.DepCode;

            
            _mysqlContext.Departments.Update(dep);
            var res = _mysqlContext.SaveChanges();
            return Json(res);
        }

        [HttpPost]
        public IActionResult DelDep(Department department)
        {
            if (department == null)
            {
                return Json(-1);
            }
            var d = _mysqlContext.Departments.Single(d =>d.DepartmentId==department.DepartmentId);
            _mysqlContext.Remove(d);
            var res = _mysqlContext.SaveChanges();
            return Json(res);
        }

        /// <summary>
        /// 部门是否已存在
        /// </summary>
        /// <param name="department"></param>
        /// <returns></returns>
        private bool IsExistDep(Department department)
        {
            var roles = _mysqlContext.Departments.Where(d => d.DepName == department.DepName||d.DepCode==department.DepCode).ToList();
            if (roles.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
